<template>
	<view class="content">
		<view class="head">
			<u-navbar leftIcon="arrow-leftward" :placeholder='true' @leftClick="leftClick" title="支付狀態">
			</u-navbar>
		</view>
		<view class="search">
			<u-search :show-action="false" borderColor="#7A7DEE" bgColor="#FFFFFF" placeholder="請輸入訂單號查詢"
				v-model="data.order_sn" @search="search"></u-search>
			<view class="title-box u-flex" @click="filter">
				<text class="filter-title">篩選</text>
				<view class="p-box u-flex-col u-m-l-6">
					<text class="iconfont icon-shaixuan"></text>
				</view>
			</view>
		</view>
		<view class="nav">
			<view :class="['nav-item', { active: currentIndex == index }]" @click="changeTab(index)"
				v-for="(item,index) in tabs" :key="index">
				{{item}}
			</view>
		</view>
		<view class="dindan" v-for="(item,index) in orderlist" :key="index">
			<view class="dindan-no">
				<view class="no-l">
					{{item.order_sn}}
				</view>
				<view class="no-r">
					<view v-if="item.pay_status == 40">
						已退款
					</view>
					<view v-else-if="item.status!=null && row.order_status==0">
						{{item.status | status}}
					</view>
					<view v-else-if="item.pay_status">
						{{item.pay_status| pay_status}}
					</view>
					<view v-else>
						{{item.order_status | orstatus}}
					</view>
				</view>
			</view>
			<view class="" @click="todetails(item)">
				<view class="goods-detail" v-for="(item,index) in item.goods " :key="index">
					<view class="detail-left">
						<view class="goods-left">
							<view class="goods-img">
								<image :src="getFirstImageLink(item.goods_images)" mode="aspectFit"></image>
							</view>
						</view>
						<view class="size">
							<view class="goods-title">{{item.goods_name }}</view>
							<view class="center">
								<text> {{item.attr_value}}</text>

								<text>x{{item.goods_number}}</text>
							</view>
							<view class="bottom">
								<text class="goods-price">MOP {{item.unit_price}}</text>
								<view class="detail-right">
								</view>
							</view>
						</view>
					</view>
				</view>
				<view class="jiage">
					共 <view style="color:#645AED ;">{{getTotalGoodsNumber(item.goods)}}</view>件，折後價格：<view
						style="color:#645AED ;">MOP
						{{item.order_amount}}
					</view>
				</view>
				<view class="jiage" v-if="item.part_pay_amount&&item.pay_status==30">
					已部分支付：<view style="color:#645AED ;">MOP
						{{item.part_pay_amount}}
					</view>
				</view>
				<view class="threebtn">
					<view class="btn" @click="refund(item)">
						申請退貨
					</view>
					<view class="btn" @click="cancel(item)">
						申請取消
					</view>
					<view class="btn green" @click="againorder(item)">
						再次下單
					</view>
				</view>
			</view>
		</view>
		<u-popup :show="show" mode="right" @close="closeWindows" @open="open">
			<view class="dialog">
				<view>
					<view class="title">訂單號</view>
					<view class="cbox">
						<u-search :showAction="false" placeholder="請輸入訂單號查詢" v-model="data.order_sn"></u-search>
					</view>
				</view>
				<view>
					<view class="title">日期</view>
					<!-- 日期选择器 -->
					<view class="cbox1">
						<view class="cbox-item" @click="toStart">
							<view class="time-item">
								{{beginTime || '开始日期'}}
							</view>
						</view>
						<view class="lines" style="color:#BEBEBE ;">-</view>
						<view class="cbox-item" @click="toEnd">
							<view class="time-item">
								{{endTime ||'结束日期'}}
							</view>
						</view>
					</view>
				</view>
				<view>
					<view class="title">部門</view>
					<view class="cel_box" @click="isPart = !isPart">
						<!-- 下拉选择器 -->
						<view class="sel_ipt">
							<u--input placeholder="请选择部门" border="none" v-model="department" shape="circle"></u--input>
						</view>
						<!-- <image src="../../../static/images/unfold_gray.png" style="width: 30rpx;height: 30rpx;"></image> -->
						<u-icon name="arrow-down" size="28rpx"></u-icon>
						<view v-if="isPart" class="part_box">
							<scroll-view v-if="isPart" scroll-y="true" style="height: 300rpx;">
								<view class="part_item" :class="actIndex==index?'act_part':''"
									v-for="(item,index) in memberlist" :key="index" @click="lookOne(item,index)">
									{{item.departmentname}}
								</view>
							</scroll-view>
						</view>
					</view>

				</view>
				<view class="btn">
					<view class="btn1" @click="reset">
						重置
					</view>
					<view class="btn2" @click="submit">
						确定
					</view>
				</view>
			</view>
		</u-popup>
		<!-- 申请退款弹窗 -->
		<u-popup :show="refundshow" :round="10" :closeable='true' mode="bottom" @close="closeWindows" @open="open">
			<view class="refund-dialog">
				<view class="title">
					申請退貨
				</view>
				<!-- <view class="text-box"> -->
				<view class="tuihuo">
					<textarea class="text-box" v-model="refundfrom.pay_note"
						placeholder-style="padding:32rpx ;font-size: 28rpx;font-family: PingFang SC;font-weight: 500;color: #999999;"
						maxlength='200' placeholder="請輸入退貨說明">
						</textarea>
					<view class="max-input">
						{{refundfrom.pay_note.length}}/200
					</view>
				</view>
				<view class="btn" @click="returnsub">
					提交
				</view>
			</view>
		</u-popup>
		<u-datetime-picker ref="datetimePicker" :show="showTime" v-model="valueStart" mode="date" :formatter="formatter"
			title="开始时间" @confirm="confirmStart" @cancel="cancelStart"></u-datetime-picker>
		<u-datetime-picker ref="datetimePicker" :show="showTime1" v-model="valueEnd" mode="date" :formatter="formatter"
			title="结束时间" @confirm="confirmEnd" @cancel="cancelEnd"></u-datetime-picker>
	</view>
</template>

<script>
	import {
		paystatus,
		myorders,
		returngood,
		cancelgood,
		orderdetils,
		againcart
	} from '@/network/api/user.js'
	import {
		getmember,
	} from '@/network/api/cart.js'
	export default {
		data() {
			return {
				refundfrom: {
					order_id: '',
					pay_note: '',
				},
				value2: "",
				refundshow: false,
				orderlist: [],
				choiceIndex: 0, //选择位置
				show: false,
				tabs: ['全部', '待支付', '已全數支付', '已部分支付'],
				currentIndex: 0,
				keyword: "",
				valueStart: Number(new Date()),
				valueEnd: Number(new Date()),
				beginTime: '',
				endTime: '',
				showTime: false,
				showTime1: false,
				department: '', //部门
				isPart: false, //选择部门
				memberlist: [],
				actIndex: 0,
				page: 1,
        data: {
          lang: 'cn',
          release: '',
          relend: '',
          order_sn: '',
          department: '',
          user_id: '',
          pay_status: '',
          sortAttr: '',
          sortMode: '',
          page: 1,
        },
        last_page: 0,
        indextab: '',
			}
		},
		components: {},
		filters: {
			// 列表订单状态过滤器
			status(
				val,
			) {
				switch (val) {
					case 0:
						return '未審批'
						break;
					case 1:
						return '已審批'
						break;
					case 2:
						return '申請修改'
						break;
					case 3:
						return '取消訂單'
						break;
					case 4:
						return '未通過'
						break;
				}
			},
			orstatus(
				val,
			) {
				switch (val) {
					case '0':
						return '無需審批'
						break;
					case '1':
						return '未付款'
						break;
					case '2':
						return '已付款'
						break;
					case '3':
						return '已確認收'
						break;
					case '4':
						return '已發貨'
						break;
					case '5':
						return '已退貨'
						break;
					case '6':
						return '全單取消'
						break;
					case '7':
						return '要求退貨'
						break;
					case '8':
						return '已退貨'
						break;
				}
			},
			pay_status(val) {
				switch (val) {
					case '0':
						return '待付款'
						break;
					case '10':
						return '逾期'
						break;
					case '20':
						return '已全數支付'
						break;
					case '30':
						return '已部分支付'
						break;
				}
			}
		},
		onReady() {
			// 微信小程序需要用此写法
			this.$refs.datetimePicker.setFormatter(this.formatter)
		},
		onShow() {
			this.data.page = 1
			this.page = 1
			this.orderlist = []
      if(this.indextab){
        this.changeTab(this.indextab)
      }else {
        this.getpaystatuslist()
      }
			this.getmemberlist()
		},
		onLoad(e) {
			if (e.indextab) {
        this.indextab=e.indextab
			}
		},
		onReachBottom() {
			console.log('我到底了');
      if(this.data.page < this.last_page) {
        this.data.page++
        this.getpaystatuslist()
      }
		},
		methods: {

			getTotalGoodsNumber(item) {
				// 计算商品总数量
				return item.reduce((total, goods) => total + goods.goods_number, 0);
			},
			async getmemberlist() {
				let company_id = uni.getStorageSync('user').userinfo.company_id
				const res = await getmember(company_id)
				this.memberlist = res.data.department
			},
			search() {
				this.orderlist = []
				this.data.page = 1
				this.getpaystatuslist()
			},
			async getpaystatuslist() {
				// 转圈
				uni.showLoading({
					title: '加载中'
				})
				const res = await paystatus(this.data)
				uni.hideLoading()
				const newpaystatuslist = res.data.order
				if (newpaystatuslist.length == 0) {
					uni.showToast({
						title: '暫無數據',
						icon: 'none',
						duration: 2000
					})
				}
        this.last_page=res.data.page.last_page
				this.orderlist = [...this.orderlist, ...newpaystatuslist]

			},
			formatter(type, value) {
				if (type === 'year') {
					return `${value}年`
				}
				if (type === 'month') {
					return `${value}月`
				}
				if (type === 'day') {
					return `${value}日`
				}
				return value
			},
			toStart() {
				this.showTime = true
				this.showTime1 = false
			},
			toEnd() {
				this.showTime1 = true
				this.showTime = false
			},
			confirmStart(e) {
				let date = new Date(e.value);
				let y = date.getFullYear();
				let MM = date.getMonth() + 1;
				MM = MM < 10 ? ('0' + MM) : MM; //月补0
				let d = date.getDate();
				d = d < 10 ? ('0' + d) : d; //天补0
				this.beginTime = y + '-' + MM + '-' + d; //年月日
				this.data.release = this.beginTime
				this.showTime = false
			},
			cancelStart(e) {
				this.showTime = false
			},
			confirmEnd(e) {
				let date = new Date(e.value);
				let y = date.getFullYear();
				let MM = date.getMonth() + 1;
				MM = MM < 10 ? ('0' + MM) : MM; //月补0
				let d = date.getDate();
				d = d < 10 ? ('0' + d) : d; //天补0
				this.endTime = y + '-' + MM + '-' + d; //年月日
				this.data.relend = this.endTime
				this.showTime1 = false
			},
			cancelEnd(e) {
				this.showTime1 = false
			},
			lookOne(item, index) {
				this.department = item.departmentname
				this.data.department = item.department_id
				this.actIndex = index
			},
			async returnsub() {
				const res = await returngood(this.refundfrom)
				if (res.data.code == 1) {
					uni.showToast({
						title: "提交申请成功，请耐心等待",
						icon: 'success',
					});
					this.refundfrom.pay_note = ''
					this.refundshow = false
				} else {
					this.refundfrom.pay_note = ''
					this.refundshow = false
				}
			},
			// 申请退款
			refund(item) {
				console.log(item);
				this.refundshow = true
				this.refundfrom.order_id = item.order_id
			},
			async cancel(item) {
				let data = {
					order_id: item.order_id,
					cancel_note: '',
				}
				const res = await cancelgood(data)
				uni.showToast({
					title: "提交申请成功，请耐心等待",
					icon: 'success',
				});
			},
			async againorder(item) {
				console.log(item);
				const from = item.goods.map(item => {
					return {
						rec_id: item.rec_id,
						order_id: item.order_id,
						goods_number: item.goods_number,
						goods_id: item.goods_id,
						goods_images: item.goods_images,
						goodnumber: item.goodnumber,
						numbering: item.numbering,
						goods_sn: item.goods_sn,
						specification_numbering: item.specification_numbering
					}
				})
				let data = {
					goods: from,
					lang: 'cn',
					order_sn: item.order_sn,
				}
				const res = await againcart(data)
				if (res.data.code == 1) {
					uni.showToast({
						title: '下单成功',
						icon: 'success',
						duration: 2000
					})
				}
			},
			todetails(item) {
				console.log(item, '1111');
				uni.navigateTo({
					url: `/pages/mine/paystatus/details?orderid=${item.order_id}&part_pay_amount=${item.part_pay_amount}`
				})
			},
			closeWindows() {
				this.show = false
				this.refundshow = false
			},
			open() {

			},
			reset() {
				this.data.order_sn = '',
					this.data.release = '',
					this.data.relend = '',
					this.data.department = '',
					this.department = '',
					this.endTime = '',
					this.beginTime = ''
				this.orderlist = []
				this.data.page = 1
				this.getpaystatuslist()
				this.show = false
			},
			submit() {
				this.orderlist = []
				this.data.page = 1
				this.getpaystatuslist()
				this.show = false
			},
			filter() {
				this.show = true
			},
			async changeTab(index) {
				this.currentIndex = index;
        this.indextab=index
				if (this.currentIndex == 0) {
					this.data.pay_status = ''
				}
				if (this.currentIndex == 1) {
					this.data.pay_status = 0
				}
				if (this.currentIndex == 2) {
					this.data.pay_status = 20
				}
				if (this.currentIndex == 3) {
					this.data.pay_status = 30
				}
        this.data.page = 1
        this.page = 1
        this.orderlist = []
				await this.getpaystatuslist()
			},
			leftClick() {
				uni.switchTab({
					url: '/pages/mine/index'
				})
			}
		}
	}
</script>

<style lang="scss">
	@import "@/static/iconfont/iconfont.css";

	page {
		background: #f5f6f9;
	}

	.bg {
		background-color: white;
	}

	.content {
		.search {
			background-color: white;
			display: flex;
			align-items: center;
			padding: 15rpx 32rpx;

			.title-box {
				display: flex;
				margin-left: 22rpx;
				align-items: center;
			}
		}

		.nav {
			background-color: white;
			height: 88rpx;
			line-height: 88rpx;
			display: flex;
			justify-content: space-around;
			font-size: 28rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #666666;
		}

		.active {
			font-size: 28rpx;
			font-family: PingFang SC;
			font-weight: bold;
			color: #1F1F1F;
			border-bottom: 5rpx solid #645AED;
		}

		.jiage {
			display: flex;
			justify-content: flex-end;
			align-items: center;
			margin-top: 22rpx;
			font-size: 28rpx;
			font-family: PingFang SC;
			font-weight: bold;
		}

		.threebtn {
			margin-top: 40rpx;
			display: flex;
			justify-content: flex-end;
			font-size: 28rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #333333;

			.btn {
				margin-left: 20rpx;
				text-align: center;
				line-height: 60rpx;
				width: 170rpx;
				height: 60rpx;
				border: 1rpx solid #CBCBCB;
				border-radius: 20rpx;
			}

			.green {

				border: 1rpx solid #645AED;
				color: #645AED;
			}
		}



		.dindan {
			margin-top: 16rpx;
			background: white;
			padding: 32rpx;

			.dindan-no {
				height: 84rpx;
				border-bottom: 1rpx solid #F1F1F1;
				display: flex;
				justify-content: space-between;
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: 500;

				.no-l {
					color: #3A3A3A;
				}

				.no-r {
					color: #645AED
				}
			}

			.goods {
				line-height: 80rpx;
				background-color: #FFFFFF;

				&-detail {
					display: flex;
					padding: 16rpx 0;
					// margin: 0 32rpx;
					background-color: #fff;
					justify-content: space-between;
					align-items: center;

					.detail-left {
						display: flex;
						flex: 1;

						.goods-left {

							display: flex;
							align-items: center;

							.goods-img {
								width: 180rpx;
								height: 180rpx;
								background: #FFFFFF;
								border: 1rpx solid #DEDEDE;
								border-radius: 30rpx;
								display: flex;
								justify-content: center;
								align-items: center;

								image {
									max-width: 90%;
									max-height: 90%;
								}
							}
						}
					}

					.size {
						flex: 1;
						display: flex;
						justify-content: space-between;
						flex-direction: column;
						margin-left: 30rpx;

						.center {
							justify-content: space-between;
							display: flex;
							font-size: 22rpx;
							font-family: PingFang SC;
							font-weight: 500;
							color: #999999;
							margin-top: 8rpx;
						}

						.bottom {
							margin-top: 22rpx;
							display: flex;
							align-items: center;
							justify-content: space-between
						}

						.goods-title {
							font-size: 28rpx;
							font-family: PingFang SC;
							font-weight: 500;
							color: #333333;
						}

						.goods-price {
							font-size: 28rpx;
							font-family: PingFang SC;
							font-weight: bold;
							color: #4949C3;
						}
					}

					.detail-right {
						.stepper {
							display: flex;
							// align-items: center;
							vertical-align: middle;

							.stepper-value {
								margin: 0 20rpx;
								font-size: 24rpx;
								line-height: 43rpx;
								font-family: PingFang SC;
								font-weight: 500;
								color: #333333;
							}

							.stepper-btn {
								image {
									width: 44rpx;
									height: 44rpx;
								}
							}
						}

						text {
							width: 50rpx;
							line-height: 50rpx;
							text-align: center;
							display: inline-block;
							background-color: #F7F7F7;
							margin-right: 10rpx;
						}

						.add {
							color: #FA4305;
							border-radius: 10rpx 30rpx 30rpx 10rpx;
							margin-right: 20rpx;
						}

						.subtract {
							border-radius: 30rpx 10rpx 10rpx 30rpx;
						}
					}
				}
			}

		}

		.main {
			margin-top: 16rpx;
			padding: 0 32rpx;

			.order-item {
				padding: 32rpx 0;
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #999999;
				border-bottom: 1rpx solid #DEDEDE;
				;
			}

			.order-item:last-child {
				border-bottom: none;
			}
		}

		.dialog {
			// padding: 0 31rpx;
			width: 640rpx;

			.title {
				margin: 0 31rpx;
				margin-top: 80rpx;
				margin-bottom: 31rpx;
				font-size: 26rpx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #000000;
			}

			.cbox {
				margin: 0 31rpx;
				display: flex;
				justify-content: space-between;
				flex-wrap: wrap;

				.citem {
					margin-bottom: 32rpx;
					text-align: center;
					align-items: center;
					width: 185rpx;
					height: 54rpx;
					line-height: 54rpx;
					background: #F5F6F9;
					border-radius: 16rpx;
					font-size: 26rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #000000;
				}

				.active {
					border: 1px solid #645AED;
					background: #E5E7FA;
					color: #645AED;
				}
			}

			.cbox1 {
				margin: 0 32rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.lines {
					padding: 10rpx;
				}

				.cbox-item {
					.time-item {
						width: 220rpx;
						background-color: #f2f2f2;
						height: 64rpx;
						border-radius: 32rpx;
						text-align: center;
						line-height: 64rpx;
						font-size: 28rpx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #999999;
					}
				}
			}

			.cel_box {
				margin-left: 32rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				width: 500rpx;
				height: 60rpx;
				border-radius: 30rpx;
				background-color: #f2f2f2;
				padding: 0 20rpx;
				position: relative;
			}

			.part_box {
				position: absolute;
				left: 20rpx;
				top: 70rpx;
				width: 400rpx;
				height: 300rpx;
				box-shadow: 0rpx -3rpx 6rpx 0rpx rgba(0, 0, 0, 0.40);

				.part_item,
				.act_part {
					width: 320rpx;
					height: 60rpx;
					font-size: 24rpx;
					color: #000;
					line-height: 60rpx;
					padding: 0 40rpx;
				}

				.act_part {
					background-color: #f2f2f2;
				}
			}

			.btn {
				position: absolute;
				left: 31rpx;
				right: 31rpx;
				bottom: 15rpx;
				display: flex;
				justify-content: space-between;

				.btn1 {
					width: 280rpx;
					height: 68rpx;
					text-align: center;
					line-height: 68rpx;
					border: 1px solid #CBCBCB;
					opacity: 1;
					border-radius: 24rpx;
					font-size: 28rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #FFFFFF;
					font-size: 28rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #3A3A3A;
				}

				.btn2 {
					width: 280rpx;
					height: 68rpx;
					text-align: center;
					background: #645AED;
					line-height: 68rpx;
					border: 1px solid #CBCBCB;
					opacity: 1;
					border-radius: 24rpx;
					font-size: 28rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #FFFFFF;
				}
			}
		}

		.refund-dialog {
			height: 900rpx;
			display: flex;
			flex-direction: column;
			align-items: center;

			.btn {
				position: fixed;
				bottom: 16rpx;
				width: 686rpx;
				height: 80rpx;
				background: #645AED;
				border-radius: 30rpx;
				text-align: center;
				line-height: 80rpx;
				font-size: 32rpx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #FFFFFF;
			}

			.title {

				margin-top: 44rpx;
				font-size: 32rpx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #333333;
			}

			.tuihuo {
				position: relative;
				width: 686rpx;
				min-height: 316rpx;
				margin: 60rpx auto;

				.text-box {
					width: 686rpx;
					height: 316rpx;
					border-radius: 20rpx;
					background: #F7F7F7;
				}

				.max-input {
					position: absolute;
					bottom: 20rpx;
					right: 32rpx;
					font-size: 24rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #333333;
				}
			}


		}
	}
</style>
